package com.jz.community.moduleshoppingguide.neighbor.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jz.community.basecomm.utils.RxDataTool;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.neighbor.bean.BlogsListBean;
import com.jz.community.moduleshoppingguide.neighbor.utils.CircleMovementMethod;
import com.jz.community.moduleshoppingguide.neighbor.utils.SpannableClickable;
import com.jz.community.moduleshoppingguide.neighbor.utils.UrlUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by yiwei on 16/7/9.
 */
public class CommentListView extends LinearLayout {
    private int itemColor;
    private int itemSelectorColor;
    private OnItemClickListener onItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;
    private List<BlogsListBean.EmbeddedBean.ContentBean.ReplyVosBean> mDatas;
    private LayoutInflater layoutInflater;

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public OnItemLongClickListener getOnItemLongClickListener() {
        return onItemLongClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public void setDatas(List<BlogsListBean.EmbeddedBean.ContentBean.ReplyVosBean> datas) {
        if (datas == null) {
            datas = new ArrayList<>();
        }
        mDatas = datas;
        notifyDataSetChanged();
    }

    public List<BlogsListBean.EmbeddedBean.ContentBean.ReplyVosBean> getDatas() {
        return mDatas;
    }

    public CommentListView(Context context) {
        super(context);
    }

    public CommentListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(attrs);
    }

    public CommentListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(attrs);
    }

    protected void initAttrs(AttributeSet attrs) {
        TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.PraiseListView, 0, 0);
        try {
            //textview的默认颜色
            itemColor = typedArray.getColor(R.styleable.PraiseListView_item_color, getResources().getColor(R.color.font_black));
            itemSelectorColor = typedArray.getColor(R.styleable.PraiseListView_item_selector_color, getResources().getColor(R.color.gray_light));
        } finally {
            typedArray.recycle();
        }
    }

    public void notifyDataSetChanged() {
        removeAllViews();
        if (mDatas == null || mDatas.size() == 0) {
            return;
        }
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        for (int i = 0; i < mDatas.size(); i++) {
            final int index = i;
            View view = getView(index);
            if (view == null) {
                throw new NullPointerException("listview item layout is null, please check getView()...");
            }
            addView(view, index, layoutParams);
        }

    }

    private View getView(final int position) {
        if (layoutInflater == null) {
            layoutInflater = LayoutInflater.from(getContext());
        }
        View convertView = layoutInflater.inflate(R.layout.module_shoppingguide_item_comment, null, false);
        TextView commentTv = convertView.findViewById(R.id.commentTv);
        CircleMovementMethod circleMovementMethod = new CircleMovementMethod(itemSelectorColor, itemSelectorColor);
        BlogsListBean.EmbeddedBean.ContentBean.ReplyVosBean bean = mDatas.get(position);
        String name = bean.getPoNickName();
        String id = String.valueOf(bean.getPoUserId());
        String toReplyName = "";
        if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(id)) {
            if (!TextUtils.isEmpty(bean.getReNickName())) {
                toReplyName = bean.getReNickName();
            }
            SpannableStringBuilder builder = new SpannableStringBuilder();
            builder.append(setClickableSpan(toReplyName, bean.getReUserId() + ""));
            if (!TextUtils.isEmpty(toReplyName)) {
                builder.append(" 回复 ");
                builder.append(setClickableSpan(name, String.valueOf(id)));
            }
            builder.append(": ");
            //转换表情字符
            String contentBodyStr = bean.getContent();
            builder.append(UrlUtils.formatUrlString(contentBodyStr));
            commentTv.setText(builder);

            commentTv.setMovementMethod(circleMovementMethod);
            commentTv.setOnClickListener(v -> {
                if (circleMovementMethod.isPassToTv()) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(v, position);
                    }
                }
            });
            commentTv.setOnLongClickListener(v -> {
                if (circleMovementMethod.isPassToTv()) {
                    if (onItemLongClickListener != null) {
                        onItemLongClickListener.onItemLongClick(v, position);
                    }
                    return true;
                }
                return false;
            });
        } else {
            if (!TextUtils.isEmpty(bean.getReNickName())) {
                SpannableStringBuilder builder = new SpannableStringBuilder();
                toReplyName = bean.getReNickName();
                if (!RxDataTool.isNullString(toReplyName)) {
                    builder.append(setClickableSpan(toReplyName, String.valueOf(bean.getReUserId())));
                }
                builder.append(": ");
                //转换表情字符
                String contentBodyStr = bean.getContent();
                builder.append(UrlUtils.formatUrlString(contentBodyStr));
                commentTv.setText(builder);
                commentTv.setMovementMethod(circleMovementMethod);
                commentTv.setOnClickListener(v -> {
                    if (circleMovementMethod.isPassToTv()) {
                        if (onItemClickListener != null) {
                            onItemClickListener.onItemClick(v, position);
                        }
                    }
                });
                commentTv.setOnLongClickListener(v -> {
                    if (circleMovementMethod.isPassToTv()) {
                        if (onItemLongClickListener != null) {
                            onItemLongClickListener.onItemLongClick(v, position);
                        }
                        return true;
                    }
                    return false;
                });
            }
        }
        return convertView;
    }

    @NonNull
    private SpannableString setClickableSpan(final String textStr, final String id) {
        SpannableString subjectSpanText = new SpannableString(textStr);
        subjectSpanText.setSpan(new SpannableClickable(itemColor) {
                                    @Override
                                    public void onClick(View widget) {

                                    }
                                }, 0, subjectSpanText.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return subjectSpanText;
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(View v, int position);
    }
}
